package com.ruihu.admin.module.business.task.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.ruihu.admin.common.anno.OperateLog;
import com.ruihu.admin.common.domain.PageResultDTO;
import com.ruihu.admin.common.controller.BaseController;
import com.ruihu.admin.common.domain.ResponseDTO;
import com.ruihu.admin.common.domain.ValidateList;
import com.ruihu.admin.module.business.task.constant.TaskStatusEnum;
import com.ruihu.admin.module.business.task.domain.dto.TaskAddDTO;
import com.ruihu.admin.module.business.task.domain.dto.TaskUpdateDTO;
import com.ruihu.admin.module.business.task.domain.dto.TaskQueryDTO;
import com.ruihu.admin.module.business.task.domain.vo.TaskVO;
import com.ruihu.admin.module.business.task.service.TaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

/**
 * [ 任务主表 ]
 *
 * @author Tony
 * @version 1.0
 * @company 瑞虎（南京）信息技术有限公司
 * @copyright (c)  瑞虎（南京）信息技术有限公司Inc. All rights reserved.
 * @date 2022-08-26 20:42:35
 * @since JDK1.8
 */
@RestController
@Api(tags = {"主任务"})
@OperateLog
public class TaskController extends BaseController {

    @Autowired
    private TaskService taskService;

    @ApiOperation(value = "分页查询任务主表",notes = "@author Tony")
    @PostMapping("/task/page/query")
    public ResponseDTO<PageResultDTO<TaskVO>> queryByPage(@RequestBody TaskQueryDTO queryDTO) {
        return taskService.queryByPage(queryDTO);
    }

    @ApiOperation(value = "分页查询正在运行的主任务",notes = "@author Tony")
    @PostMapping("/task/start/page/query")
    public ResponseDTO<PageResultDTO<TaskVO>> queryStartTaskByPage(@RequestBody @Validated TaskQueryDTO queryDTO) {
        if (StrUtil.isEmpty(queryDTO.getStatus()) && CollectionUtil.isEmpty(queryDTO.getStatusList())) {
            // 默认查询创建、运行中、暂停、完成的任务
            List<String> statusList = new ArrayList<>();
            statusList.add(TaskStatusEnum.CREATED.getValue());
            statusList.add(TaskStatusEnum.RUNNING.getValue());
            statusList.add(TaskStatusEnum.PAUSE.getValue());
            statusList.add(TaskStatusEnum.COMPLETE.getValue());
            queryDTO.setStatusList(statusList);
        }
        return taskService.queryByPage(queryDTO);
    }

    @GetMapping("/task/query/{taskId}")
    public ResponseDTO<TaskVO> query(@PathVariable("taskId")Long taskId) {
        return taskService.query(taskId);
    }

    @ApiOperation(value = "添加主任务",notes = "@author Tony")
    @PostMapping("/task/add")
    public ResponseDTO<String> add(@RequestBody @Validated TaskAddDTO addTO){
        return taskService.add(addTO);
    }

    @ApiOperation(value="修改主任务",notes = "@author Tony")
    @PostMapping("/task/update")
    public ResponseDTO<String> update(@RequestBody @Validated TaskUpdateDTO updateDTO){
        return taskService.update(updateDTO);
    }

    @ApiOperation(value="启动主任务",notes = "@author Tony")
    @PostMapping("/task/start/{taskId}")
    public ResponseDTO<String> startTask(@PathVariable("taskId") Long taskId){
        return taskService.startTask(taskId);
    }

    @ApiOperation(value="暂停主任务",notes = "@author Tony")
    @PostMapping("/task/pause/{taskId}")
    public ResponseDTO<String> pauseTask(@PathVariable("taskId") Long taskId){
        return taskService.pauseTask(taskId);
    }

    @ApiOperation(value="批量删除主任务",notes = "@author Tony")
    @PostMapping("/task/deleteByIds")
    public ResponseDTO<String> delete(@RequestBody @Validated ValidateList<Long> idList) {
        return taskService.deleteByIds(idList);
    }

    @ApiOperation(value="删除主任务",notes = "@author Tony")
    @PostMapping("/task/{taskId}/delete")
    public ResponseDTO<String> deleteById(@PathVariable("taskId") Long taskId) {
        return taskService.deleteById(taskId);
    }

}
